package com.chinatechstar.decis.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinatechstar.decis.entity.DecisMonthEffect;
import com.chinatechstar.decis.mapper.DecisMonthEffectMapper;
import com.chinatechstar.decis.service.DecisMonthEffectService;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 业务逻辑实现层
 *
 * @author Rei
 */
@Service
@Transactional
public class DecisMonthEffectServiceImpl implements DecisMonthEffectService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final SequenceGenerator SEQUENCE_GENERATOR = new SequenceGenerator();

    @Autowired
    private DecisMonthEffectMapper decisMonthEffectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询所有数据
     */
    @Override
    public List<DecisMonthEffect> findDecisMonthEffectAll() {
        return decisMonthEffectMapper.findDecisMonthEffectAll();
    }

    /**
     * 查询分页
     */
    @Override
    public Map<String, Object> findDecisMonthEffectByCondition(Integer currentPage, Integer pageSize, String name, String sorter) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        if (StringUtils.isNotBlank(sorter)) {
            String sort = sorter.substring(0, sorter.lastIndexOf('_'));
            String sequence = "ascend".equals(sorter.substring(sorter.lastIndexOf('_') + 1)) ? "ASC" : "DESC";
            paramMap.put("sort", sort);
            paramMap.put("sequence", sequence);
        } else {
            paramMap.put("sort", "createTime");
            paramMap.put("sequence", "DESC");
        }
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList = decisMonthEffectMapper.findDecisMonthEffectByCondition(paramMap);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), currentPage, pageSize);
    }

    /**
     * 根据主键ID数据查询数据
     */
    @Override
    public DecisMonthEffect findDecisMonthEffectById(Long id) {
        return decisMonthEffectMapper.findDecisMonthEffectById(id);
    }

    /**
     * 根据主键ID数组查询数据
     */
    @Override
    public List<DecisMonthEffect> findDecisMonthEffectByIds(Long[] ids) {
        return decisMonthEffectMapper.findDecisMonthEffectByIds(ids);
    }

    /**
     * 新增
     */
    @Override
    public void insertDecisMonthEffect(DecisMonthEffect decisMonthEffect) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        decisMonthEffect.setId(SEQUENCE_GENERATOR.nextId());
        decisMonthEffect.setCreateName(currUser.getNickname());
        decisMonthEffect.setCreateUsername(currUser.getUsername());
        decisMonthEffectMapper.insertDecisMonthEffect(decisMonthEffect);
        logger.info("已新增： {}", decisMonthEffect.getId());
    }

    /**
     * 批量新增
     */
    @Override
    public void insertDecisMonthEffects(List<DecisMonthEffect> decisMonthEffects) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        for (DecisMonthEffect decisMonthEffect : decisMonthEffects) {
            decisMonthEffect.setId(SEQUENCE_GENERATOR.nextId());
            decisMonthEffect.setCreateName(currUser.getNickname());
            decisMonthEffect.setCreateUsername(currUser.getUsername());
        }
        decisMonthEffectMapper.insertDecisMonthEffects(decisMonthEffects);
    }

    /**
     * 编辑
     */
    @Override
    public void updateDecisMonthEffect(DecisMonthEffect decisMonthEffect) {
        decisMonthEffectMapper.updateDecisMonthEffect(decisMonthEffect);
        logger.info("已编辑： {}", decisMonthEffect.getId());
    }

    /**
     * 批量编辑
     */
    @Override
    public void updateDecisMonthEffects(List<DecisMonthEffect> decisMonthEffects) {
        decisMonthEffectMapper.updateDecisMonthEffects(decisMonthEffects);
    }

    /**
     * 删除
     */
    @Override
    public void deleteDecisMonthEffectById(Long id) {
        decisMonthEffectMapper.deleteDecisMonthEffectById(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteDecisMonthEffectByIds(Long[] ids) {
        decisMonthEffectMapper.deleteDecisMonthEffectByIds(ids);
    }

}
